package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import util.DBUtil;

import dao.TaxDao;

public class TaxDaoJdbcImpl implements TaxDao {

	@Override
	public double getTaxRate(String productName) throws Exception {
		String sql="select taxrate from tax where productname=?";
		Connection con=null;
		try {
			con=DBUtil.openInThread();
			PreparedStatement pst=con.prepareStatement(sql);
			pst.setString(1, productName);
			ResultSet  rs=pst.executeQuery();
			double taxRate=-1;
			if(rs.next()){
				taxRate=rs.getDouble("taxrate");
			}
			DBUtil.close(null, pst, rs);
			return taxRate;
		} finally {
			DBUtil.closeInThread();
		}
	}
	
	public static void main(String[] args) throws Exception {
		TaxDaoJdbcImpl dao= new TaxDaoJdbcImpl();
		double rate=dao.getTaxRate("iphone");
		System.out.println(rate);
	}

}
